package com.jetgege.smartbox.Tools;

/**
 * Created by JJT-ssd on 2017/3/27.
 */

public class Agreement {

    //get runing state
    public static byte[] runState={0x01,00,00,00,00,00,00,(byte)0xff};

    //start run
    public static byte[] startRun={0x02,00,00,00,00,00,00,(byte)0xfe};

    //set working voltage
    public static byte[] setVoltage(int value){

        byte[] data= {0x03,(byte)value,00,00,00,00,00};
        return createAgreement(data);
    }

    //协议生成工厂
    private static byte[] createAgreement( byte[] bytes)
    {
        int sum=0;
        int len=bytes.length;
        //1.计算校验和
        for (int i=0;i<len;i++)
            sum+=bytes[i];
        //2.生成校验码
        int checkNum = 0-sum;

        byte[] result =new byte[len+1];
        for (int j=0;j<len;j++)
            result[j]=bytes[j];
        result[len]=(byte) checkNum;
        return result;
    }

    /**
     * 数据校验
     * @param dataBuff
     * @return 正确返回 true 否则返回false
     */
    public static boolean dataCheck(byte dataBuff[])
    {
        int sum=0;//校验和
        int len= dataBuff.length;
        //2.2数据校验
        //计算校验和
        for (int i = 0; i <len ; ++i) {
            sum= sum+(dataBuff[i]&255);
        }
        //和校验
        if(sum%256!=0) return false;
        return true;
    }
}
